from django import forms
from django.forms import formset_factory
from scaffold_toolkit.forms import KindEditor
from basedata.models import BaseData, City
from product.models import House, HouseImage


class HouseRentForm(forms.ModelForm):
    info = forms.CharField(required=False, label='描述', widget=KindEditor)
    position = forms.ChoiceField(choices=BaseData.array_to_tuple(BaseData.position_array), label='朝向', required=True)
    decorate = forms.ChoiceField(choices=BaseData.array_to_tuple(BaseData.decorate_array), label='装修情况', required=True)
    rent_way = forms.ChoiceField(choices=BaseData.array_to_tuple(BaseData.rent_way_array), label='租住方式', required=True)
    type = forms.ChoiceField(choices=BaseData.array_to_tuple(BaseData.house_type_array), label='房屋类型', required=True)
    layout = forms.ChoiceField(choices=BaseData.array_to_tuple(BaseData.house_layout_array), label='布局', required=True)
    area_name = forms.CharField(label='城市', max_length=2, required=False)

    class Meta:
        model = House
        fields = ['name', 'address', 'rent_money', 'size', 'position', 'decorate', 'rent_way', 'type',
                  'layout', 'floor_num', 'floor_total', 'info']

    def save(self, commit=True):
        instance = super(HouseRentForm, self).save(False)
        instance.category = 'R'
        # print(self.cleaned_data['area_name'] + '----------------')
        city_id=int(self.cleaned_data['area_name'])
        city = City(id=city_id)
        instance.city = city
        if instance.id:
            instance.save()
        else:
            instance.save()
        if self.formset:
            HouseImage.objects.filter(house_id=instance.id).delete()
        for form in self.formset:
            if form.clean():
                house_pic = form.cleaned_data['house_pic']
                HouseImage.objects.create(house_pic=house_pic, house=instance)
                return instance


class HouseRentSetForm(forms.Form):
    house_pic = forms.ImageField(label='房子照片', required=False)


FormSet = formset_factory(HouseRentSetForm, extra=2, validate_min=False)


class HouseSailForm(forms.ModelForm):
    info = forms.CharField(required=False, label='描述', widget=KindEditor)
    position = forms.ChoiceField(choices=BaseData.array_to_tuple(BaseData.position_array), label='朝向', required=True)
    decorate = forms.ChoiceField(choices=BaseData.array_to_tuple(BaseData.decorate_array), label='装修情况', required=True)
    type = forms.ChoiceField(choices=BaseData.array_to_tuple(BaseData.house_type_array), label='房屋类型', required=True)
    layout = forms.ChoiceField(choices=BaseData.array_to_tuple(BaseData.house_layout_array), label='布局', required=True)
    area_name = forms.CharField(label='城市', max_length=2, required=False)

    class Meta:
        model = House
        fields = ['name', 'address', 'rent_money', 'size', 'position', 'decorate', 'type',
                  'layout', 'floor_num', 'floor_total', 'info']

    def save(self, commit=True):
        instance = super(HouseSailForm, self).save(False)
        instance.category = 'S'
        city_id=int(self.cleaned_data['area_name'])
        city = City(id=city_id)
        instance.city = city
        if instance.id:
            instance.save()
        else:
            instance.save()
        if self.formset:
            HouseImage.objects.filter(house_id=instance.id).delete()
        for form in self.formset:
            if form.clean():
                house_pic = form.cleaned_data['house_pic']
                HouseImage.objects.create(house_pic=house_pic, house=instance)
        return instance


class HouseSailSetForm(forms.Form):
    house_pic = forms.ImageField(label='房子照片', required=False)


Form_Set = formset_factory(HouseSailSetForm, extra=2,validate_min=False)


class SearchForm(forms.Form):
    name = forms.CharField(required=False)
